package com.cet.designpatterns.iterator;

import java.util.Iterator;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-09-21 23:15
 **/
public class ComputerCollege implements College {

    private Department[] departments;

    private int num = 0;

    public ComputerCollege() {
        departments = new Department[1];
    }

    @Override
    public String getName() {
        return "计算机学院";
    }

    @Override
    public void addDepartment(String name, String desc) {
        if (num == departments.length) {
            resize(2 * departments.length);
        }
        Department department = new Department(name, desc);
        departments[num++] = department;
    }

    @Override
    public Iterator createIterator() {
        return new ComputerCollegeIterator(departments);
    }

    private void resize(int max) {
        Department[] temp = new Department[max];
        System.arraycopy(departments, 0, temp, 0, num);
        departments = temp;
    }
}
